<?php

namespace app\admin\controller;

use My\MyClass;
use think\Db;
use think\Request;

class User extends MyClass
{
    public function index()
    {
        return view('admin@User/tables');
    }

    public function User()
    {

        //查询所有用户
        $row= Db::table('user1')->field(['id','name'])->order('id asc')->select();
        //查询所有角色
        $result = Db::field(['u.id'=>'uid','u.name'=>'un','j.name'=>'jn'])
            ->table(['user1'=>'u','jiaose'=>'j','user_j'=>'uj'])
            ->where('u.id=uj.uid and j.id=uj.jid')
            ->order('uid asc')
            ->select();

        //绑定角色到新数组
        $arr = [];
        foreach ($row as $k=>$v){
            foreach($result as $val){
                if($v['id']==$val['uid']){
                    $v['role'][]=$val['jn'];
                    $arr[$k] = $v;
                }
            }
        }

        return view('admin@user/user',['data'=>$arr]);
    }

    public function read($id)
    {
       $result = Db::field(['u.name'=>'un','j.name'=>'jn'])
           ->table(['user1'=>'u','jiaose'=>'j','user_j'=>'uj'])
           ->where('u.id=uj.uid and j.id=uj.jid and u.id ='.$id)
           ->select();

       if ($result == null){
           $this->error('当前用户不具备任何角色!');
       }
        $name = null;
        $jiaose = null;

        for ($i=0;$i<count($result);$i++){
           $name = $result['0']['un'];
           $jiaose .= $result[$i]['jn'].'-';
       }
        $jiaose = substr($jiaose,0,-1);

        return view('admin@User/jiaose',[
            'name'=>$name,
            'jiaose'=>$jiaose
        ]);

    }
    public function add()
    {
        return view('admin@User/add');
    }

    public function doadd(Request $request)
    {

        $result = $request->post();

        $data = [
            'name'=>$result['name'],
            'pass'=>md5($result['pass'])
        ];

        $row = Db::table('user1')->InsertGetId($data);
        $res = Db::table('user_j')->insert(['uid'=>$row,'jid'=>'9']);

        if ($row >0 && $res>0){
            return $this->success('添加成功!','/users/user');
        }else{
            return $this->error('添加失败,请重试!');
        }

    }

    public function show($id)
    {
        $row = Db::field(['u.id'=>'uid','u.name'=>'un','j.name'=>'jn'])
            ->table(['user1'=>'u','jiaose'=>'j','user_j'=>'uj'])
            ->where('u.id=uj.uid and j.id=uj.jid and u.id ='.$id)
            ->order('uid desc')
            ->select();

        if ($row == null){
            $result = Db::table('jiaose')->select();
            return view('admin@User/null',[
                'data'=>$result,
                'id'=>$id

            ]);
        }

        $arr=array();
        foreach ($row as $key => $value) {
            if( in_array($value['un'], $value)){
                $arr[$value['un']][]=$value['jn'];
                $arr1 = $arr[$value['un']];
            }
        }

        $result = Db::table('jiaose')->select();
//        halt($result);


        return view('admin@User/give',[
            'data'=>$result,
            'jiaose'  =>$arr1,
            'id'=>$id

        ]);
    }


    public function addjs(Request $request,$id)
    {
        $p = $request->post();

        if ($p == null){
            return $this->error('您没有添加任何角色!');
        }


//        halt($p);
        $abc = [];

        foreach ($p as $k=>$v){

            foreach ($v as $val){

                $abc[] = ['uid'=>$id,'jid'=>$val];
            }
        }

        //添加之前先删除
        Db::table('user_j')->where('uid='.$id)->delete();
        for ($i=0;$i<count($abc);$i++){
            $row = Db::table('user_j')->insert($abc[$i]);
        }
        if ($row >0){
            return $this->success('添加成功!','/users/user');
        }else{
            return $this->error('添加失败,请重试!');
        }
    }
}